<template>
  <el-row class="treetools" style="width: 100%">
    <el-col :span="12">
      <i class="el-icon-s-home" />
      {{ data.name }}
    </el-col>
    <el-col :span="12" style="text-align: right">
      <span class="manager">{{ data.manager }}</span>
      <el-dropdown @command="myCommand">
        <span class="el-dropdown-link">
          操作<i class="el-icon-arrow-down el-icon--right" />
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="add">添加子部门</el-dropdown-item>
          <el-dropdown-item v-if="isHeader === false" command="edit">查看部门</el-dropdown-item>
          <el-dropdown-item v-if="isHeader === false" command="del">删除部门</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </el-col>
  </el-row>
</template>

<script>
import { delDeptApi } from '@/api/departments.js'
export default {
  props: ['data', 'isHeader'],
  methods: {
    async myCommand(command) {
      if (command === 'add') {
        // 增加
        this.$emit('addShow', this.data.id)
      } else if (command === 'edit') {
        // 查找
        this.$emit('editShow', this.data.id)
      } else if (command === 'del') {
        // 删除
        const res = await this.$confirm('此操作将永久删除该部门, 是否继续?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
        if (res === 'confirm') {
          delDeptApi(this.data.id)
          this.$emit('getNewData')
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.treetools {
  .manager {
    margin-right: 10px;
  }
}
</style>
